<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
  		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>网站后台管理模版</title>
		<link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}"/>
		<link rel="stylesheet" type="text/css" th:href="@{/css/admin.css}"/>
		<link th:href="@{/lib/bootstraps/css/bootstrap.css}" rel="stylesheet" />
		<link th:href="@{/lib/bootstraps/css/zTreeStyle/zTreeStyle.css}" rel="stylesheet" />
	</head>
	<body>
	<form  class="layui-form" action="" method="post" id="assignRoleForm">
		<div class="modal-body">
			<input type="hidden" th:value="${userId}" id="assignRoleUserId"/>
			<div style="margin-left: 30%" align="right" id="treeDemo" class="ztree" ></div>
		</div>
		<div class="modal-footer">
			<button type="button" class="btn btn-default"
					data-dismiss="modal">关闭
			</button>
			<button  class="btn btn-primary" type="button" onclick="submitAssignRole()">
				提交
			</button>
		</div>
	</form>
	<script th:src="@{/js/jquery-3.2.1.min.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
	<script th:src="@{/js/common.js}" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript" th:src="@{/lib/bootstraps/js/ztree/jquery.ztree.core-3.5.min.js}"></script>
	<script type="text/javascript" th:src="@{/lib/bootstraps/js/ztree/jquery.ztree.excheck-3.5.min.js}"></script>
	<script>

		$(function () {
            var setting = {
                check: {
                    enable: true,
                    autoCheckTrigger: true
                },
                data: {
                    simpleData: {
                        enable: true
                    }
                },
                async: {
                    enable: true,
                    url: "/getRoleTree?userId="+$('#assignRoleUserId').val(),
                    autoParam: ["id", "name"]
                }
            };
            $.fn.zTree.init($("#treeDemo"), setting);//加载树
        });

		layui.use(['form', 'table', 'laydate', 'layer', 'laypage', 'dialog',  'tool', 'element', 'upload', 'layedit'], function() {
				var form = layui.form,
					layer = layui.layer,
                    table = layui.table,
					laypage = layui.laypage,
					laydate = layui.laydate,
					layedit = layui.layedit,
					tool = layui.tool,
					element = layui.element,
					dialog = layui.dialog;

				//获取当前iframe的name值
				var iframeObj = $(window.frameElement).attr('name');

			});


        function submitAssignRole(){
            var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
            var userId = $('#assignRoleUserId').val();
            var nodes = treeObj.getCheckedNodes(true);
            var roles = new Array();
            for(var i=0;i<nodes.length;i++){
                roles.push(nodes[i].id)
            }
            if(nodes.length === 0){
                roles.push('0')
            }
            $.ajax({
                type:"post",
                url:"/addOrUpdateRoleAssign",
                dataType:'json',
                async:true,
                data:{userId:userId,roles:roles},
                success:function(data){
                   layer.msg("分配成功");
                    var index = parent.layer.getFrameIndex(window.name);
                    setTimeout(function () {
                        //先得到当前iframe层的索引
                        parent.layer.close(index);
                        //window.parent.location.replace(parent.location.href)
                        parent.layui.table.reload('test',{page:{curr:1}});
                    },3000);
                },error:function(){
                    layer.msg("添加失败");
                }
            });
        }

	</script>
	</body>
</html>